package org.foj.model {
import com.anywebcam.mock.Mockery;

import flash.events.Event;
import flash.events.EventDispatcher;

import mx.managers.CursorManager;
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;

import org.flexunit.async.Async;
import org.foj.dto.Issue;
import org.foj.event.EventDispatcherFactory;

public class IssueModelTest {

  private var target:IssueModel;
  private var service:MockIssueService;
  private var token:AsyncToken;
  private var responder:IResponder;
  private var mockery:Mockery;
  private var dispatcher:EventDispatcher;

  public function IssueModelTest() {
  }

  [Before(async,timeout=5000)]
  public function setUp():void {
    mockery = new Mockery();
    mockery.prepare(MockIssueService);
    mockery.prepare(AsyncToken);
    mockery.prepare(IResponder);

    dispatcher = EventDispatcherFactory.getEventDispatcher();

    Async.proceedOnEvent(this, mockery, Event.COMPLETE, 5000);
  }

  [After]
  public function tearDown():void {
    CursorManager.removeBusyCursor();
    mockery.verify(service, token);
  }

  [Test]
  public function callsGetAllOnRemoteService():void {
    service = mockery.strict(MockIssueService) as MockIssueService;
    token = mockery.strict(AsyncToken)as AsyncToken;
    responder = mockery.strict(IResponder) as IResponder;

    mockery.mock(service).method("getAll").withNoArgs.returns(token).once;
    mockery.mock(token).method("addResponder").withArgs(responder).once;

    target = new IssueModel(service);

    target.getIssues(responder);
  }

  [Test]
  public function getIssue():void {
    service = mockery.strict(MockIssueService) as MockIssueService;
    token = mockery.strict(AsyncToken)as AsyncToken;
    responder = mockery.strict(IResponder) as IResponder;

    var id:Number = 42;

    mockery.mock(service).method("get").withArgs(id).returns(token).once;
    mockery.mock(token).method("addResponder").withArgs(IResponder).once;

    target = new IssueModel(service);

    target.getIssue(id, responder);
  }

  [Test]
  public function saveIssue():void {
    service = mockery.strict(MockIssueService) as MockIssueService;
    token = mockery.strict(AsyncToken)as AsyncToken;
    responder = mockery.strict(IResponder) as IResponder;

    var issue:Issue = new Issue();

    mockery.mock(service).method("save").withArgs(issue).returns(token).once;
    mockery.mock(token).method("addResponder").withArgs(IResponder).once;

    target = new IssueModel(service);

    target.saveIssue(issue, responder);
  }

  [Test]
  public function removeIssue():void {
    service = mockery.strict(MockIssueService) as MockIssueService;
    token = mockery.strict(AsyncToken)as AsyncToken;
    responder = mockery.strict(IResponder) as IResponder;

    var id:Number = 42;

    mockery.mock(service).method("remove").withArgs(id).returns(token).once;
    mockery.mock(token).method("addResponder").withArgs(IResponder).once;

    target = new IssueModel(service);

    target.removeIssue(id, responder);
  }

}
}